Skip to content

[AUTOMATION] fix: optimize internal/guard/policy evaluation category checks#241

Open
michiosw wants to merge 1 commit into
mainfrom
fix/optimize-policy-category-checks
Open

[AUTOMATION] fix: optimize internal/guard/policy evaluation category checks#241
michiosw wants to merge 1 commit into
mainfrom
fix/optimize-policy-category-checks

Conversation

@michiosw
Copy link
Copy Markdown
Contributor

@michiosw michiosw commented Jun 1, 2026

Summary

This optimizes deterministic policy evaluation by reusing the enabled category set for the full rule pass.

Before this, internal/guard/policy/engine.go rebuilt the same profile category map for every rule check, which added repeated allocations on every hook decision.

Now one enabled-category map is built once per evaluation and reused across the rule loop.

Why

This gives kontext-cli a cheaper runtime path for deterministic guard policy decisions:

hook event
-> internal/guard/policy.Engine.Evaluate
-> one profile category map + rule matching result

This PR does not broaden behavior beyond the optimization scope.

What changed

Optimized deterministic policy category checks in internal/guard/policy/engine.go
Removed repeated enabled-category map construction during the per-rule loop
Preserved existing profile gating and rule matching behavior
Used existing policy tests to verify behavior did not change

Verification

go test ./internal/guard/policy
go test ./internal/guard/judge -run TestStartLlamaServerHealthCheckAndStop -count=1 -v
go test ./internal/guard/judge -count=1
go test ./...
go vet ./...
git diff --check

Copy link
Copy Markdown
Contributor Author

michiosw commented Jun 1, 2026

This stack of pull requests is managed by Graphite. Learn more about stacking.

@greptile-apps
Copy link
Copy Markdown

greptile-apps Bot commented Jun 1, 2026

Greptile Summary

This PR optimizes deterministic guard policy evaluation by reusing the enabled category set. The main changes are:

  • Builds the enabled category map once per Engine.Evaluate call.
  • Uses the cached category map for each rule check.
  • Removes the now-unused unexported categoryEnabled helper.

Confidence Score: 5/5

This looks safe to merge.

  • No blocking issues found in the changed code.

Important Files Changed

Filename Overview
internal/guard/policy/engine.go Moves enabled category map construction outside the rule loop without changing rule matching behavior.
internal/guard/policy/profiles.go Removes an unexported helper after its only use was replaced by the cached map lookup.

Reviews (1): Last reviewed commit: "fix(policy): reuse enabled category map ..." | Re-trigger Greptile

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant